precision mediump float;// 数据精度
//.xy
varying mediump vec2 aCoord;


//需要外部赋值一个采样器给这个变量
//2d采样器，有0-31个图层
uniform sampler2D vTexture;

//采样20个像素
vec2 blurCoordinates[20];

uniform int vWidth;
uniform int vHeight;

void main(){

    //如果是 width=1080 height 1920

    //步长 [1/1080,1/1920]
    vec2 singleStepOffset=vec2(1.0/float(vWidth), 1.0/float(vHeight));

    //对角线采样
    blurCoordinates[0]=aCoord.xy+singleStepOffset*vec2(0.0, -10.0);
    blurCoordinates[1]=aCoord.xy+singleStepOffset*vec2(0.0, 10.0);
    blurCoordinates[2]=aCoord.xy+singleStepOffset*vec2(-10.0, 0.0);
    blurCoordinates[3]=aCoord.xy+singleStepOffset*vec2(10.0, 0.0);
    //-------------------------------------------------------------
    blurCoordinates[4]=aCoord.xy+singleStepOffset*vec2(5.0, -8.0);
    blurCoordinates[5]=aCoord.xy+singleStepOffset*vec2(5.0, 8.0);
    blurCoordinates[6]=aCoord.xy+singleStepOffset*vec2(-5.0, 8.0);
    blurCoordinates[7]=aCoord.xy+singleStepOffset*vec2(-5.0, -8.0);
    //-------------------------------------------------------------
    blurCoordinates[8]=aCoord.xy+singleStepOffset*vec2(8.0, -5.0);
    blurCoordinates[9]=aCoord.xy+singleStepOffset*vec2(8.0, 5.0);
    blurCoordinates[10]=aCoord.xy+singleStepOffset*vec2(-8.0, 5.0);
    blurCoordinates[11]=aCoord.xy+singleStepOffset*vec2(-8.0, -5.0);
    //-------------------------------------------------------------
    blurCoordinates[12]=aCoord.xy+singleStepOffset*vec2(0.0, -6.0);
    blurCoordinates[13]=aCoord.xy+singleStepOffset*vec2(0.0, 6.0);
    blurCoordinates[14]=aCoord.xy+singleStepOffset*vec2(6.0, 0.0);
    blurCoordinates[15]=aCoord.xy+singleStepOffset*vec2(-6.0, 0.0);
    //-------------------------------------------------------------
    blurCoordinates[16]=aCoord.xy+singleStepOffset*vec2(-4.0, -4.0);
    blurCoordinates[17]=aCoord.xy+singleStepOffset*vec2(-4.0, 4.0);
    blurCoordinates[18]=aCoord.xy+singleStepOffset*vec2(4.0, -4.0);
    blurCoordinates[19]=aCoord.xy+singleStepOffset*vec2(4.0, 4.0);

    vec4 currentColor=texture2D(vTexture, aCoord);
    vec3 rgb=currentColor.rgb;
    for (int i=0;i<20;i++){
        //当前点加上周围20个值
        rgb+=texture2D(vTexture, blurCoordinates[i].xy).rgb;
    }
    //求平均值
    vec4 blur=vec4(rgb*1.0/21.0, currentColor.a);

    //    gl_FragColor=blur;

    //--------------------------------------------
    //原图减去模糊后的，差值越小，说明是轮廓会显示出来
    vec4 highPassColor=currentColor-blur;
    //    gl_FragColor=highPassColor;
    //--------------------------------------------

    //抛物线？色阶开平方？然后通过clamp函数限制范围，取0-1范围内，小于0则为0，大于1则为1
    //为了更好地突出轮廓细节
    highPassColor.r=clamp(2.0*highPassColor.r*highPassColor.r*24.0,0.0,1.0);
    highPassColor.g=clamp(2.0*highPassColor.g*highPassColor.g*24.0,0.0,1.0);
    highPassColor.b=clamp(2.0*highPassColor.b*highPassColor.b*24.0,0.0,1.0);
//    gl_FragColor=highPassColor;

    //取出蓝色通道
    //currentColor.b原图颜色-----高blur.b斯模糊颜色
    float b=min(currentColor.b,blur.b);

    //线性叠加
    float value=clamp((b-0.2)*5.0,0.0,1.0);
    //取rgb最大值,为了保留细节，因为细节部分颜色比较深
    float maxChannelColor=max(highPassColor.r,max(highPassColor.g,highPassColor.b));


    //磨皮程度
    float intensity=1.0;//0-1.0 再大就模糊了
    //细节地方不融合，不细节地方（额头，痘印等）使劲融合
    float currentIntensity=(1.0-maxChannelColor/(maxChannelColor+0.2))*value*intensity;


    vec3 r=mix(currentColor.rgb,blur.rgb,currentIntensity);

    gl_FragColor=vec4(r,currentColor.a);

}